<!-- 仓库 - 工程数据 - 发货计划 -->
<template>
  <basic-container>
    <avue-crud
      :option="option"
      :table-loading="loading"
      :data="data"
      :page.sync="page"
      :search.sync="search"
      :before-open="beforeOpen"
      :before-close="beforeClose"
      :summary-method="summaryMethod"
      v-model="form"
      ref="crud"
      @search-change="searchChange"
      @search-reset="searchReset"
      @selection-change="selectionChange"
      @current-change="currentChange"
      @size-change="sizeChange"
      @refresh-change="refreshChange"
      @on-load="onLoad"
    >
      <template slot="menuRight">
        <user-table-config :option="option" @confirm="(val) => handleSetUserConfig(val, pageCode)"></user-table-config>
      </template>
      <template slot="menuLeft">
        <el-button type="warning" size="small" icon="el-icon-download" @click="$refs.exportComp.visible = true">导出</el-button>
      </template>
      <template slot="infoForm">
        <avue-crud ref="infoCrud" :option="infoOption" :data="infoData" v-model="infoForm"></avue-crud>
      </template>
    </avue-crud>
    <export-comp
      ref="exportComp"
      page-api-url="mes-product/finished-inventory-report/page-inventory-age"
      :option="option"
      :page="page"
      :query="query"
      :dataListChain="'page'"
      file-name="WIP查询"
    ></export-comp>
  </basic-container>
</template>

<script>
import { getList, getDetail, computeQuantity } from '@/api/warehouse/inventory-quantity-wip';
import { mapGetters } from 'vuex';
import userTableConfig from '@/components/user-table-config/index.vue';
import exportComp from '@/components/export-comp';
export default {
  components: {
    exportComp,
    userTableConfig
  },
  data() {
    return {
      pageCode: 'inventoryQuantityWip',
      visible: false,
      form: {},
      data: [],
      query: {},
      search: {},
      loading: true,
      page: {
        pageSize: 20,
        currentPage: 1,
        total: 0
      },
      selectionList: [],
      option: {
        height: 'calc(100vh - 440px)',
        tip: false,
        searchShow: true,
        searchSpan: 5,
        searchMenuSpan: 4,
        labelWidth: 110,
        dialogCustomClass: 'custom-class',
        border: true,
        index: true,
        indexLabel: '序号',
        addBtn: false,
        viewBtn: true,
        viewBtnText: '详情',
        viewTitle: '详情',
        editBtn: false,
        delBtn: false,
        refreshBtn: true,
        excelBtn: false,
        selection: true,
        dialogDrag: false, // 开启拖拽功能
        dialogClickModal: false,
        columnBtn: false,
        showSummary: true,
        column: [
          {
            label: '工单号',
            prop: 'workOrderNo',
            search: true,
            viewDisplay: false
          },
          {
            label: '产品编码',
            prop: 'productPartNo',
            search: true,
            viewDisplay: false
          },
          {
            label: '产品名称',
            prop: 'productPartName',
            viewDisplay: false
          },
          {
            label: '产品型号',
            prop: 'productPartModel',
            search: true,
            viewDisplay: false
          },
          {
            label: '客户',
            prop: 'customerName',
            search: true,
            viewDisplay: false
          },
          {
            label: '车间',
            prop: 'workshopName',
            type: 'select',
            search: true,
            dicUrl: '/api/mes-product/workshop/page?current=1&size=10000',
            props: {
              label: 'name',
              value: 'id'
            },
            dicFormatter: (data) => {
              return data.data.records;
            },
            viewDisplay: false
          },
          {
            label: '产线',
            prop: 'lineName',
            dicUrl: '/api/mes-product/product/line/page?current=1&size=10000',
            type: 'select',
            search: true,
            props: {
              label: 'name',
              value: 'id'
            },
            viewDisplay: false,
            dicFormatter: (data) => {
              return data.data.records;
            }
          },
          {
            label: '产线派工数量',
            prop: 'linePlanQuantity',
            viewDisplay: false
          },
          {
            label: '产线完工数量',
            prop: 'lineActualQuantity',
            viewDisplay: false
          },
          {
            label: '产线未完工数量',
            prop: 'lineUnfinishQuantity',
            viewDisplay: false
          }
        ],
        group: [
          {
            label: '基本信息',
            prop: 'baseInfo',
            arrow: false,
            icon: 'el-icon-vertical-bar',
            addDisplay: false,
            editDisplay: false,
            column: [
              {
                label: '工单号',
                prop: 'workOrderNo',
                span: 8
              },
              {
                label: '工单计划开始时间',
                prop: 'planStartTime',
                span: 8
              },
              {
                label: '产线派工数量',
                prop: 'linePlanQuantity',
                span: 8
              },
              {
                label: '产品名称',
                prop: 'productPartName',
                span: 8
              },
              {
                label: '工单计划结束时间',
                prop: 'planEndTime',
                span: 8
              },
              {
                label: '产线完工数量',
                prop: 'lineActualQuantity',
                span: 8
              },
              {
                label: '产品编码',
                prop: 'productPartNo',
                span: 8
              },
              {
                label: '工单计划数量',
                prop: 'planQuantity',
                span: 8
              },
              {
                label: '产线未完工数量',
                prop: 'lineUnfinishQuantity',
                span: 8
              },
              {
                label: '产品型号',
                prop: 'productPartModel',
                span: 8
              },
              {
                label: '车间',
                prop: 'workshopName',
                span: 8
              },
              // {
              //   label: '',
              //   prop: '',
              //   span: 8
              // },
              {
                label: '客户',
                prop: 'customerName',
                span: 8
              },
              {
                label: '产线',
                prop: 'lineName',
                span: 8
              }
            ]
          },
          {
            label: '在制品信息',
            prop: 'wipInfo',
            arrow: false,
            icon: 'el-icon-vertical-bar',
            addDisplay: false,
            editDisplay: false,
            column: [
              {
                labelWidth: 0,
                label: '',
                prop: 'info',
                span: 24,
                hide: true,
                formslot: true
              }
            ]
          }
        ]
      },
      infoForm: {},
      infoOption: {
        selection: false,
        editBtn: false,
        height: 270,
        border: true,
        menuWidth: 100,
        addBtn: false,
        delBtn: true,
        menu: false,
        columnBtn: false,
        refreshBtn: false,
        tip: false,
        column: [
          {
            label: '工序',
            prop: 'routeName'
          },
          {
            label: '数量',
            prop: 'totalQuantity'
          },
          {
            label: '已过站数',
            prop: 'passQuantity'
          },
          {
            label: '待过站数',
            prop: 'waitingQuantity'
          }
        ]
      },
      infoData: [],
      allCountAmount: ''
    };
  },
  watch: {
    // 'search.workshopId'(val) {
    //   this.search.lineName = !val ? this.search.lineName : '';
    //   const column = this.findObject(this.option.column, 'lineName');
    //   let params = {};
    //   if (val) {
    //     params['workshopId'] = val;
    //   }
    //   getProductionlineList(1, 10000, params)
    //     .then((res) => {
    //       column.dicData = this._get(res, 'data.data.records', []);
    //       column.dicData = this._map(column.dicData, (item) => {
    //         return {
    //           label: item.name,
    //           value: item.name
    //         };
    //       });
    //     })
    //     .catch((err) => {
    //       column.dicData = [];
    //     });
    // }
  },
  mounted() {
    this.$nextTick(() => {
      this.$structuralSplit();
    });
    this.computeTotalQuantity();
  },
  computed: {
    ...mapGetters(['permission']),
    ids() {
      let ids = [];
      this.selectionList.forEach((ele) => {
        ids.push(ele.id);
      });
      return ids.join(',');
    }
  },
  methods: {
    computeTotalQuantity() {
      computeQuantity().then((res) => {
        this.allCountAmount = res.data.data;
      });
    },
    closeDialog() {
      this.leftSelectionList = [];
      this.rightSelectionList = [];
      this.showDialog = false;
    },
    search() {
      this.getSelectLineRequest(this.leftForm);
    },
    beforeOpen(done, type) {
      if (['view'].includes(type)) {
        getDetail({
          lineId: this.form.lineId,
          workOrderId: this.form.workOrderId
        }).then((res) => {
          this.infoData = res.data.data;
        });
        done();
      }
    },
    searchReset() {
      this.query = {};
      this.onLoad(this.page);
    },
    searchChange(params, done) {
      if (params.workshopName) {
        params.workshopId = params.workshopName;
        delete params.workshopName;
      }
      if (params.lineName) {
        params.lineId = params.lineName;
        delete params.lineName;
      }
      this.query = params;
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      done();
    },
    selectionChange(list) {
      this.selectionList = list;
    },
    selectionClear() {
      this.selectionList = [];
      this.$refs.crud.toggleSelection();
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },
    refreshChange() {
      this.onLoad(this.page, this.query);
    },
    onLoad(page, params = {}) {
      this.loading = true;
      getList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then((res) => {
        const data = this._get(res, 'data.data', {});
        this.page.total = data.total;
        this.data = data.records;
        this.loading = false;
        this.selectionClear();
      });
    },
    summaryMethod({ columns, data }) {
      const sums = [];
      if (columns.length > 0) {
        columns.forEach((column, index) => {
          let prop = column.property;
          if (prop === 'lineUnfinishQuantity') {
            sums[index] = `合计：${this.allCountAmount}`;
          } else {
            sums[index] = '';
          }
        });
      }
      return sums;
    }
  }
};
</script>

<style lang="scss">
.btn {
  padding: 9px 15px;
  font-size: 10px;
  height: 35px;
  background-color: #409eff !important;
  color: #cccccc !important;
}

.is-disabled {
  background-color: #fff !important;
  color: #cccccc !important;
}

.title {
  color: #000;
}

.title:hover {
  color: #000;
}

.el-transfer-button {
  display: flex;
  flex-direction: row;
  height: 100%;
  margin: 40px 12px 0;
  align-items: center;
}

.search-container {
  display: flex;
  justify-content: space-between;
}

.search-item {
  display: flex;
  flex-direction: row;
  align-items: center;
}
.el-color-picker__trigger {
  height: 35px;
  width: 35px;
}
</style>
